package cn.wizzer.app.web.modules.controllers.user;

import cn.wizzer.app.user.modules.models.UserAddress;
import cn.wizzer.app.user.modules.services.UserAddressService;
import cn.wizzer.app.web.commons.filter.ApiTokenFilter;
import cn.wizzer.app.web.commons.ulog.annotation.ULog;
import cn.wizzer.framework.base.Result;
import cn.wizzer.framework.filter.ParamFilter;
import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.annotation.*;

@Api(value = "/api/user/address", tags = "收货地址")
@IocBean
@At("/api/user/address")
public class ApiUserAddressController {
    @Inject
    @Reference(retries = 3, timeout = 6000)
    private UserAddressService addressService;

    /**
     * 收货地址列表(分页)
     *
     * @param pageNumber int
     * @param pageSize   int
     * @return Object
     */
    @At("/list")
    @Ok("json")
    @Filters({@By(type = ApiTokenFilter.class), @By(type = ParamFilter.class)})
    public Object list(@Param("pageNumber") int pageNumber, @Param("pageSize") int pageSize) {
        String userId = Mvcs.getReq().getHeader("appid");
        return Result.success().addData(addressService.listPage(pageNumber, pageSize, userId));
    }


    /**
     * 添加收货地址
     *
     * @param userAddress UserAddress
     * @return Object
     */
    @At("/add")
    @Ok("json")
    @Filters({@By(type = ApiTokenFilter.class), @By(type = ParamFilter.class)})
    @ULog(tag = "用户添加收货地址")
    public Object add(@Param("..") UserAddress userAddress) {
        String appid = Mvcs.getReq().getHeader("appid");
        Result result = addressService.validate(userAddress);
        if (result.getCode() == 0) {
            userAddress.setUserId(appid);
            addressService.insert(userAddress);
        }
        return result;
    }

    /**
     * 删除收货地址
     *
     * @param id String 地址id
     * @return Object
     */
    @At("/delete")
    @Ok("json")
    @Filters({@By(type = ApiTokenFilter.class), @By(type = ParamFilter.class)})
    @ULog(tag = "用户删除收货地址")
    public Object delete(@ApiParam(required = true) @Param("id") String id) {
        addressService.update(Chain.make("delFlag", true), Cnd.where("id", "=", id));
        return Result.success();
    }

    /**
     * 设置默认收货地址
     *
     * @param id    String 地址id
     * @return Object
     */
    @At("/setDefault")
    @Ok("json")
    @Filters({@By(type = ApiTokenFilter.class), @By(type = ParamFilter.class)})
    @ULog(tag = "用户设置默认收货地址")
    public Object setDefault(@ApiParam(required = true) @Param("id") String id) {
        String appid = Mvcs.getReq().getHeader("appid");
        addressService.setDefault(id, appid);
        return Result.success();
    }


/*
    // 修改收货地址(暂无修改功能)
    @At("/editDo")
    @Ok("json")
    public Object editDo(@Param("..") UserAddress userAddress) {
        try {
            addressService.update(userAddress);
            return Result.success();
        } catch (Exception e) {
            return Result.error();
        }
    }
    // id查询收货地址
    @At("/edit/?")
    @Ok("json")
    public Object edit(String id) {
        try {
            return Result.success(addressService.fetch(id));
        } catch (Exception e) {
            return Result.error();
        }
    }

 */
}
